﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class EditCustomerOrderDetails :BasePage
    {
        int id;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!string.IsNullOrEmpty(Request.QueryString[ID]))
                id = int.Parse(Request.QueryString[ID]);
            else
                id = 0;

            if (!IsPostBack)
                LoadInfo();
        }

        OrderItems orderDetails;

        private void LoadInfo()
        {
            orderDetails = new OrderItems();
            orderDetails.LoadByPrimaryKey(id);
            if (orderDetails.RowCount <= 0)
                return;

            string productName = Products.GetProductNameById(orderDetails.ProductID);
            editDetails.ProductName = productName;

            if (!orderDetails.IsColumnNull("Quantity"))
                editDetails.Quantity = orderDetails.Quantity;
            if (!orderDetails.IsColumnNull("GST"))
                editDetails.GST = orderDetails.GST;
            if (!orderDetails.IsColumnNull("TotalAmount"))
                editDetails.Total = orderDetails.TotalAmount;
            if (!orderDetails.IsColumnNull("UnitPrice"))
                editDetails.UnitPrice = orderDetails.UnitPrice;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            if (orderDetails != null)
            {
                if (Save())
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
            }
        }

        private bool Save()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();

            try
            {
                tx.BeginTransaction();

                orderDetails = new OrderItems();

                orderDetails.LoadByPrimaryKey(id);
                if (orderDetails.RowCount <= 0)
                    return false;

                POSSystem.BLL.CustomerOrders order = new POSSystem.BLL.CustomerOrders();
                order.LoadByPrimaryKey(orderDetails.OrderID);

                decimal oldGST = orderDetails.GST;
                decimal oldAmount = orderDetails.TotalAmount;

                orderDetails.Quantity = editDetails.Quantity;
                orderDetails.TotalAmount = Math.Round(orderDetails.Quantity * orderDetails.UnitPrice, 2);
                orderDetails.GST = oldGST == 0 ? 0 : Math.Round(orderDetails.TotalAmount * SysVars.GST / (SysVars.GST + 100), 2);

                orderDetails.LastModifiedByUser = SessionUser.ID;
                orderDetails.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);

                orderDetails.Save();

                order.TotalAmount += orderDetails.TotalAmount - oldAmount;
                order.GST += orderDetails.GST - oldGST;
                order.LastModifiedByUser = SessionUser.ID;
                order.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                order.Save();

                tx.CommitTransaction();

                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("EditCustomerOrderDetails.aspx.cs", "Save", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage(ex.Message);
                return false;
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}